package com.vnp.vchat.adapter;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.vnp.vchat.R;
import com.vnp.vchat.UIControls.MyCircleImageView;
import com.vnp.vchat.Util.Utils;
import com.vnp.vchat.bean.ObjKhachhang;
import com.vnp.vchat.bean.ObjTinnhan;

import android.content.Context;
import android.graphics.BitmapFactory.Options;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TinnhanAdapter extends ArrayAdapter<ObjKhachhang> {

	Context context;
	ArrayList<ObjKhachhang> objKhachhangs = new ArrayList<ObjKhachhang>();
	Options decodingOptions = new Options();
	DisplayImageOptions options = new DisplayImageOptions.Builder()
								.cacheInMemory(true)
								.cacheOnDisc(true)
								.showImageForEmptyUri(R.drawable.defaul_avatar)
								.showImageOnFail(R.drawable.defaul_avatar)
								.bitmapConfig(Config.RGB_565)
								.build();

	public TinnhanAdapter(Context context, ArrayList<ObjKhachhang> objKhachhangs) {
		super(context, R.layout.item_tinnhan, objKhachhangs);
		this.context = context;
		this.objKhachhangs = objKhachhangs;
	}

	@Override
	public int getCount() {
		return objKhachhangs.size();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	

	@Override
	public ObjKhachhang getItem(int position) {
		return objKhachhangs.get(position);
	}

	@Override
	public View getView(int position, View v, ViewGroup parent) {
		Holder holder = null;
		if (v==null){
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.item_tinnhan, parent, false);
			holder = new Holder();

			
			holder.imgAvatar = (MyCircleImageView) v.findViewById(R.id.img_avatar);
			holder.txtTen = (TextView) v.findViewById(R.id.txt_ten);
			holder.txtLastMsg = (TextView) v.findViewById(R.id.txt_last_msg);
			holder.txtTime = (TextView) v.findViewById(R.id.txt_time_ago);
			
			v.setTag(holder);
		} else {
			holder = (Holder) v.getTag();
		}
		
		// set font
		holder.txtTen.setTypeface(Utils.getTypefaceRegular(context), Typeface.BOLD);
		holder.txtLastMsg.setTypeface(Utils.getTypefaceLight(context), Typeface.BOLD);
		holder.txtTime.setTypeface(Utils.getTypefaceRegular(context), Typeface.BOLD);
		
		ObjKhachhang objKhachhang = objKhachhangs.get(position);
		
		ImageLoader.getInstance().displayImage(objKhachhang.getUrlAvatar(), holder.imgAvatar, options);
		
		if (objKhachhang.getName().length() > 20){
			holder.txtTen.setText(objKhachhang.getName().substring(0, 17)+"...");
		} else {
			holder.txtTen.setText(objKhachhang.getName());
		}
		try {
			if (!objKhachhang.getLastMessage().equals(null)){
				if (objKhachhang.getLastMessage().length() > 30){
					holder.txtLastMsg.setText(objKhachhang.getLastMessage().substring(0, 27)+"...");
				} else {
					holder.txtLastMsg.setText(objKhachhang.getLastMessage());	
				}
			} else {
				
			}
		} catch (Exception e) {
		e.printStackTrace();
			
		}
		
		
		
		Log.i("vChat", ""+objKhachhang.getTimeLastMessage());
		
		try {
			if (objKhachhang.getTimeLastMessage().equals("")){
				holder.txtTime.setText("");
			} else {
				holder.txtTime.setText(Utils.convertStringToDate(objKhachhang.getTimeLastMessage()));
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (objKhachhang.getIsRead() == 1){
			holder.txtLastMsg.setTextColor(context.getResources().getColor(R.color.color_xanh_chu_dao));
			holder.txtTime.setTextColor(context.getResources().getColor(R.color.color_xanh_chu_dao));
		} else {
			holder.txtLastMsg.setTextColor(context.getResources().getColor(R.color.color_last_msg));
			holder.txtTime.setTextColor(context.getResources().getColor(R.color.color_last_msg));
		}
		
		
		
		return v;
	}
	
	class Holder{
		MyCircleImageView imgAvatar;
		TextView txtTen;
		TextView txtLastMsg;
		TextView txtTime;
	}
	
	

}
